﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;
using BullTasDesaDal.Service;
using BullTasDesaDal.Entities;
using System.Collections.Specialized;

namespace WSBulTasDesa.UserControls
{
    /// <summary>
    /// Descripción breve de Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {

        [WebMethod]
        public CascadingDropDownNameValue[] GetProvinces(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

            ICollection<Province> lProvinces = new List<Province>();
            lProvinces = CachingHelper.LoadProvinces();

            foreach (Province province in lProvinces)
            {
                values.Add(new CascadingDropDownNameValue(province.Name, province.Id.ToString()));
            }

            return values.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetDepartments(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
            ICollection<Department> lDepartments = new List<Department>();

            StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            short idProvince = Convert.ToInt16(categoryValues["Province"]);

            lDepartments = LocationService.GetDepartmentByProvince(idProvince);

            foreach (Department department in lDepartments)
            {
                values.Add(new CascadingDropDownNameValue(department.Name, department.Id.ToString()));
            }

            return values.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetLocalitys(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
            ICollection<Locality> lLocalitys = new List<Locality>();

            StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            int idDepartment = Convert.ToInt32(categoryValues["Department"]);

            lLocalitys = LocationService.GetLocalitysByDepartment(idDepartment);

            foreach (Locality locality in lLocalitys)
            {
                values.Add(new CascadingDropDownNameValue(locality.Name, locality.Id.ToString()));
            }

            return values.ToArray();

        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetRegions(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

            ICollection<Region> lRegions = new List<Region>();
            lRegions = CachingHelper.LoadRegions();

            foreach (Region region in lRegions)
            {
                values.Add(new CascadingDropDownNameValue(region.Name, region.Id));
            }

            return values.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetZones(string knownCategoryValues, string category)
        {
            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
            ICollection<Zone> lzones = new List<Zone>();

            StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            lzones = RegionService.GetZoneByRegion(categoryValues["Region"].ToString());

            foreach (Zone zone in lzones)
            {
                values.Add(new CascadingDropDownNameValue(zone.Name, zone.Id));
            }

            return values.ToArray();

        }

    }
}
